package com.hexu.event.dao;

import com.hexu.event.entity.User;
import com.hexu.event.util.Base64Util;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.hexu.event.util.Base64Util.decode;


public class UserDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    //检索是否存在一样的账号
    public boolean existAccount(String account){
        String sql = "SELECT account FROM USER UNION ALL SELECT account FROM administrator";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                if(account.equals(rs.getString("account").toString())) {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, rs);
        }

        return true;
    }

    //检索是否存在一样的用户名
    public boolean existUserName(String userName){
        String sql = "SELECT user_name FROM USER where user_name=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            rs = ps.executeQuery();

            while (rs.next()) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, rs);
        }

        return true;
    }

    //增加用户
    public boolean addUser(User user){
        String sql = "insert into user(account,password,user_name,service_worker)"
                + " values (?,?,?,?)";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //Base64 加密
            //View层set的数据
            ps.setString(1, user.getAccount());
            ps.setString(2, Base64Util.encrypt(user.getPassword()));
            ps.setString(3, user.getUserName());
            ps.setInt(4, user.getServiceWork());
            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }
        //添加失败
        return false;
    }

    //用户登录验证
    public boolean loginUser(String account,String password){
        String sql="select * from user where account=? and password=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            ps.setString(2,Base64Util.encrypt(password));
            rs = ps.executeQuery();

            while(rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return false;
    }

    //注销用户
    public boolean deleteUser(String account){
        String sql="delete from user where account=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,account);
            if(ps.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
        return false;
    }

    //修改用户信息
    public boolean updateUser(User user){
        String sql = "update user set password=?,user_name=?,service_worker=? where account=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, Base64Util.encrypt(user.getPassword()));
            ps.setString(2,user.getUserName());
            ps.setInt(3,user.getServiceWork());
            ps.setString(4, user.getAccount());
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //读取单个用户信息
    public User getUser(String account){
        String sql = "select * from User where account=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,account);
            rs = ps.executeQuery();
            User user = new User();
            while (rs.next()){
                user.setId(rs.getInt("id"));
                user.setPassword(decode(rs.getString("password")));
                user.setUserName(rs.getString("user_name"));
                user.setAccount(account);
                user.setServiceWork(rs.getInt("service_worker"));
            }
            return user;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //返回所有服务工作者
    public List<User> getWorker(){
        List<User> userList = new ArrayList<>();
        String sql = "select * from User where service_worker=1";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setPassword(decode(rs.getString("password")));
                user.setUserName(rs.getString("user_name"));
                user.setAccount(rs.getString("account"));
                user.setServiceWork(rs.getInt("service_worker"));

                userList.add(user);
            }
            return userList;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }
}
